#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <dirent.h>
#include <time.h>

#include "bash.h"
#include "pack.h"
#include "mypthread.h"
#include "file.h"

int i;

void init_i()
{
	srand(time(NULL));
	i = rand();
}

int PackBroadcast(char *s,char *buf,value *p)
{
	int size,l;	
	i++;
	//memset(buf,0,sizeof(buf));
	size = sprintf(buf,"%s:%u:%s:%s:%s:","5.1.180210",i,"diwutiantang","diwutiantang-VirtualBox",s);
	l = sprintf(buf+size,"%s%c%s%c%s%c%c%s%c%s%c%c5%c",p->name+5,0,p->bm+3,0,"08-00-27-58-36-73",0,0,p->dh+3,0,p->yx+3,0,0,0);
	size += l;
	l = sprintf(buf+size,"%s%c%c13%c10000001%c%s%c%s",p->IP+3,0,0,0,0,p->gxqm+5,0,"diwutiantang-VirtualBox");
	size += l;
	return size;
}

int PackChat_8e6(char *buf)
{
	int size;
	i++;
	//memset(buf,0,sizeof(buf));
	size = sprintf(buf,"%s:%u:%s:%s:2147484046:%s","5.1.180210",i,"diwutiantang","diwutiantang-VirtualBox","6.1.123");
	return size;
}

int PackChat_8e5(char *buf)
{
	int size;
	i++;
	//memset(buf,0,sizeof(buf));
	size = sprintf(buf,"%s:%u:%s:%s:2147484046:%s","5.1.180210",i,"diwutiantang","diwutiantang-VirtualBox","5.1.123");
	return size;
}

int PackChat_8e_ture(char *buf,pack *r)
{
	int size;
	i++;
	//memset(buf,0,sizeof(buf));
	size = sprintf(buf,"%s:%u:%s:%s:33:%s","5.1.180210",i,"diwutiantang","diwutiantang-VirtualBox",r->packnum);
	return size;
}

int PackChat_e7(char *buf,pack *r)
{
	int size;
	i++;
	//memset(buf,0,sizeof(buf));
	size = sprintf(buf,"%s:%u:%s:%s:487:%s","5.1.180210",i,"diwutiantang","diwutiantang-VirtualBox",r->Mac);
	return size;
}

int PackChat_288(char *buf,char *new_value)
{
	int size;
	i++;
	//memset(buf,0,sizeof(buf));
	size = sprintf(buf,"%s:%u:%s:%s:288:%s[rich]0A0000000000860008AE5F6F8FC596D19E12000000000000000000000000000000000000[/rich]","5.1.180210",i,"diwutiantang","diwutiantang-VirtualBox",new_value);
	return size;
}

int Packfile_2097440(char *buf,char *new_value)
{
	int size;
	i += 2;
	//memset(buf,0,sizeof(buf));

	struct stat st;
	stat(new_value,&st);
	size = sprintf(buf,"%s:%u:%s:%s:2097440:%c%u:%s:%lx:0:1:%lu:%lu:%lu:%c","5.1.180210",i+1,"diwutiantang","diwutiantang-VirtualBox",0,i,new_value,st.st_size,st.st_ctime,st.st_mtime,st.st_atime,0);
	return size;
}

int Packfile_96(char *buf,file_download_upload a)
{
	int size;
	i++;
	int k;
	char s1[30],s2[30];
	sscanf(a.packnum,"%d",&k);
	sprintf(s1,"%x",k);
	sprintf(s2,"%x",k-1);

	for (int b = 0;b < strlen(s1);b++)
	{
		if(s1[b] >= 'a' && s1[b] <= 'f')
			s1[b] = s1[b]-32;
	}
	for (int b = 0;b < strlen(s2);b++)
	{
		if(s2[b] >= 'a' && s2[b] <= 'f')
			s2[b] = s2[b]-32;
	}

	//memset(buf,0,sizeof(buf));
	size = sprintf(buf,"%s:%u:%s:%s:96:%s:%s:0:","1.1.180210",i,"diwutiantang","diwutiantang-VirtualBox",s1,s2);
	return size;
}

